Code
import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from gurobipy import *
'ticks')
sns.set_style(%matplotlib inline
'figure.dpi'] = 70
plt.rcParams[
= {'S1':4500, 'S2':4400, 'S3':4250, 'S4':4250}
fixed_cost = {'S1':600, 'S2':700, 'S3':500, 'S4':550}
capacity = {'D1':100, 'D2':150, 'D3':175, 'D4':125,
demand 'D5':180, 'D6':140, 'D7':120, 'D8':160}
= [26, 27, 28, 17, 13, 19, 21, 19, 19, 22, 12,
trans_cost 26, 11, 22, 22, 24, 17, 15, 13, 15, 25, 26,
27, 23, 20, 19, 23, 26, 21, 16, 23, 26]
= list(fixed_cost.keys())
suppliers = list(demand.keys())
customers = dict(zip([(i,j) for i in customers for j in suppliers],trans_cost))
trans_cost # np.array(list(trans_cost.values())).reshape(len(customers), -1).T
= Model()
m = m.addVars(suppliers, vtype=GRB.BINARY, name='y')
y = m.addVars(customers, suppliers, vtype=GRB.CONTINUOUS, name='x')
x
sum(fixed_cost[j]*y[j] for j in suppliers) +
m.setObjective(sum(trans_cost[i,j]*x[i,j] for i in customers for j in suppliers))
for i in customers:
sum(x[i,j] for j in suppliers) == demand[i])
m.addConstr(for j in suppliers:
sum(x[i,j] for i in customers) <= capacity[j]*y[j])
m.addConstr(
# m.Params.OutputFlag = 0
m.optimize()= 1
m.Params.OutputFlag 'X') m.printAttr(
Set parameter Username
Academic license - for non-commercial use only - expires 2023-11-04
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 12 rows, 36 columns and 68 nonzeros
Model fingerprint: 0x5385f77b
Variable types: 32 continuous, 4 integer (4 binary)
Coefficient statistics:
Matrix range [1e+00, 7e+02]
Objective range [1e+01, 5e+03]
Bounds range [1e+00, 1e+00]
RHS range [1e+02, 2e+02]
Presolve time: 0.01s
Presolved: 12 rows, 36 columns, 68 nonzeros
Variable types: 32 continuous, 4 integer (4 binary)
Found heuristic solution: objective 34925.000000
Root relaxation: objective 2.618097e+04, 7 iterations, 0.00 seconds (0.00 work units)
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
0 0 26180.9740 0 4 34925.0000 26180.9740 25.0% - 0s
H 0 0 29280.000000 26180.9740 10.6% - 0s
0 0 28490.0000 0 3 29280.0000 28490.0000 2.70% - 0s
0 0 29280.0000 0 2 29280.0000 29280.0000 0.00% - 0s
Cutting planes:
Gomory: 2
Implied bound: 4
Explored 1 nodes (19 simplex iterations) in 0.08 seconds (0.00 work units)
Thread count was 16 (of 16 available processors)
Solution count 2: 29280 34925
Optimal solution found (tolerance 1.00e-04)
Best objective 2.928000000000e+04, best bound 2.928000000000e+04, gap 0.0000%
Variable X
-------------------------
y[S1] 1
y[S2] 1
x[D1,S1] 10
x[D1,S2] 90
x[D2,S1] 150
x[D3,S1] 175
x[D4,S1] 125
x[D5,S2] 180
x[D6,S1] 140
x[D7,S2] 120
x[D8,S2] 160